#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "ex1.h"

char* AllocateAndCopyString(const char* src);

int* encryptToInt(char input[], int length, int first, EncryptionFuncIntType f)
{
	int i;
	int *output;
	if (length < 1)  return NULL;
	output = (int*) malloc(sizeof(int) * length);
	output[0] = f(first, input[0]);
	if (length == 1) return output;
	output[1] = f(first, input[1]);
	for (i = 2; i < length; i++)
	{
		output[i] = f(output[i-2], input[i]);
	}
	return output;
}

char* encryptToChar(char input[], int length, char first, EncryptionFuncCharType f)
{
	int i;
	char *output;
	if (length < 1)  return NULL;
	output = (char*) malloc(sizeof(char) * length);
	output[0] = f(first, input[0]);
	if (length == 1) return output;
	output[1] = f(first, input[1]);
	for (i = 2; i < length; i++)
	{
		output[i] = f(output[i-2], input[i]);
	}
	return output;

}

char** encryptToString(char input[], int length, char* first, EncryptionFuncStringType f)
{
	int i;
	char** output;
	if (length < 1)  return NULL;
	output = (char**) malloc(sizeof(char*) * length);
	output[0] = AllocateAndCopyString( f(first, input[0]));
	if (length == 1) return output;
	output[1] = AllocateAndCopyString(f(first, input[1]));
	for (i = 2; i < length; i++)
	{
		output[i] = AllocateAndCopyString(f(output[i-2], input[i]));
	}
	return output;
}

char* AllocateAndCopyString(const char* src)
{
	char* output = (char*) malloc(sizeof(char) * (strlen(src) + 1));
	strcpy(output, src);
	return output;
}